package com.sskj.mine;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckedTextView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.sskj.common.util.ClickUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.AppDirectCheckUtil;
import com.sskj.lib.util.AppOuterCheckUtil;
import com.sskj.mine.bean.PayWayBean;
import com.sskj.mine.component.DaggerUserDataComponent;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author Hey
 * Create at  2019/05/01
 */
@Route(path = RConfig.MINE_PAYMENT)

public class PaymentActivity extends BaseActivity<PaymentPresenter> {


    @BindView(R2.id.right_tv)
    TextView rightTv;
    @BindView(R2.id.no_payment_tip)
    TextView noPaymentTip;
    @BindView(R2.id.wechat_name)
    TextView wechatName;
    @BindView(R2.id.mineTextview)
    TextView mineTextview;
    @BindView(R2.id.wx_name)
    TextView wxName;
    @BindView(R2.id.wechat_account)
    TextView wechatAccount;
    @BindView(R2.id.wx_add)
    ImageView wxAdd;
    @BindView(R2.id.wechat_layout)
    ConstraintLayout wechatLayout;
    @BindView(R2.id.ali_name)
    TextView aliName;
    @BindView(R2.id.mineTextview1)
    TextView mineTextview1;
    @BindView(R2.id.aliPay_name)
    TextView aliPayName;
    @BindView(R2.id.ali_account)
    TextView aliAccount;
    @BindView(R2.id.ali_add)
    ImageView aliAdd;
    @BindView(R2.id.ali_layout)
    ConstraintLayout aliLayout;
    @BindView(R2.id.bank_name)
    TextView bankName;
    @BindView(R2.id.mineTextview2)
    TextView mineTextview2;
    @BindView(R2.id.bank_name2)
    TextView bankName2;
    @BindView(R2.id.bank_account)
    TextView bankAccount;
    @BindView(R2.id.bank_add)
    ImageView bankAdd;
    @BindView(R2.id.bank_layout)
    ConstraintLayout bankLayout;
    @BindView(R2.id.wx_edit)
    ImageView wxEdit;
    @BindView(R2.id.alipay_edit)
    ImageView aliPayEdit;
    @BindView(R2.id.bank_edit)
    ImageView bankEdit;
    @BindView(R2.id.item_switch)
    CheckedTextView itemSwitch; // wx
    @BindView(R2.id.item_switch2)
    CheckedTextView itemSwitch2;//alipay
    @BindView(R2.id.item_switch3)
    CheckedTextView itemSwitch3;//bank
    @BindView(R2.id.llSwitch)
    LinearLayout llSwitch; // wx
    @BindView(R2.id.llSwitch2)
    LinearLayout llSwitch2;//alipay
    @BindView(R2.id.llSwitch3)
    LinearLayout llSwitch3;//bank
    private PayWayBean payWayBean;
    @Inject
    UserViewModel userViewModel;
    private UserData userData;

    @Override
    public int getLayoutId() {
        return R.layout.mine_activity_payment;
    }

    @Override
    public PaymentPresenter getPresenter() {
        return new PaymentPresenter();
    }

    @Override
    public void initView() {
        setTitle(getString(R.string.mine_paymentActivity1));
        DaggerUserDataComponent.create().inject(this);
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
            } else {
                userData = null;
            }
        });
        //添加支付方式
        ClickUtil.click(wxAdd, () -> {
            if (!AppOuterCheckUtil.checkTradePwd(this, userData)) {
                return;
            }
            AddPaymentActivity.start(this, "add", "wx");
        });
        ClickUtil.click(aliAdd, () -> {
            if (!AppOuterCheckUtil.checkTradePwd(this, userData)) {
                return;
            }
            AddPaymentActivity.start(this, "add", "alipay");
        });
        ClickUtil.click(bankAdd, () -> {
            if (!AppOuterCheckUtil.checkTradePwd(this, userData)) {
                return;
            }
            AddPaymentActivity.start(this, "add", "backcard");
        });
        ClickUtil.click(wxEdit, () -> AddPaymentActivity.start(this, "edit", "wx"));
        ClickUtil.click(aliPayEdit, () -> AddPaymentActivity.start(this, "edit", "alipay"));
        ClickUtil.click(bankEdit, () -> AddPaymentActivity.start(this, "edit", "backcard"));
        ClickUtil.click(itemSwitch, () -> {
            mPresenter.payWayOpen("wx", payWayBean.getWx().getStatus() == 0 ? 1 : 0);
        });
        ClickUtil.click(itemSwitch2, () -> {
            mPresenter.payWayOpen("alipay", payWayBean.getAlipay().getStatus() == 0 ? 1 : 0);
        });
        ClickUtil.click(itemSwitch3, () -> {
            mPresenter.payWayOpen("backcard", payWayBean.getBackcard().getStatus() == 0 ? 1 : 0);
        });
    }

    @Override
    public void initData() {

    }

    public static void start(Context context) {
        Intent intent = new Intent(context, PaymentActivity.class);
        context.startActivity(intent);
    }

    public void updateUi(PayWayBean payWayBean) {
        this.payWayBean = payWayBean;
        if (!TextUtils.isEmpty(payWayBean.getWx().getNumber())) {  // 微信
            wxAdd.setVisibility(View.GONE);
            itemSwitch.setVisibility(View.VISIBLE);
            itemSwitch.setChecked(payWayBean.getWx().getStatus() == 0 ? false : true);
            wxEdit.setVisibility(View.VISIBLE);
            wxName.setText(payWayBean.getWx().getName());
            wxName.setVisibility(View.VISIBLE);
            wechatAccount.setText(payWayBean.getWx().getNumber());
            wechatAccount.setVisibility(View.VISIBLE);
        }
        if (!TextUtils.isEmpty(payWayBean.getAlipay().getNumber())) {  // 支付宝
            aliAdd.setVisibility(View.GONE);
            itemSwitch2.setVisibility(View.VISIBLE);
            aliPayEdit.setVisibility(View.VISIBLE);
            itemSwitch2.setChecked(payWayBean.getAlipay().getStatus() == 0 ? false : true);
            aliPayName.setText(payWayBean.getAlipay().getName());
            aliPayName.setVisibility(View.VISIBLE);
            aliAccount.setText(payWayBean.getAlipay().getNumber());
            aliAccount.setVisibility(View.VISIBLE);
        }
        if (!TextUtils.isEmpty(payWayBean.getBackcard().getNumber())) {  // 银行卡
            bankAdd.setVisibility(View.GONE);
            bankEdit.setVisibility(View.VISIBLE);
            itemSwitch3.setVisibility(View.VISIBLE);
            itemSwitch3.setChecked(payWayBean.getBackcard().getStatus() == 0 ? false : true);
            bankName2.setText(payWayBean.getWx().getName());
            bankName2.setVisibility(View.VISIBLE);
            bankAccount.setText(payWayBean.getBackcard().getNumber());
            bankAccount.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.requsetPayWayList();
    }


}
